<?php
namespace Wxpay\Controller;
use Think\Controller;
vendor('Wxpay.lib.WxPay#Api');
class QueryController extends Controller
{
    protected function log(){
        $log_addr = DOC_ROOT."/Public/logs/".date('Y-m-d').'.log';
        $log = R('Common/log', array($log_addr));
        return $log;
    }
    /**--订单查询--**/
    public function wxorder(){
//        ini_set('date.timezone','Asia/Shanghai');
        error_reporting(E_ERROR);

        $transaction_id = I('transaction_id');
        $out_trade_no = I('out_trade_no');

        if($transaction_id != "" || $out_trade_no != ""){
            if($transaction_id != ""){
                $transaction_id = $_REQUEST["transaction_id"];
                $input = new \WxPayOrderQuery();
                $input->SetTransaction_id($transaction_id);
//                exit();
            }

            if($out_trade_no != ""){
                $out_trade_no = $_REQUEST["out_trade_no"];
                $input = new \WxPayOrderQuery();
                $input->SetOut_trade_no($out_trade_no);
//                exit();
            }
            $result = \WxPayApi::orderQuery($input);
            R('Common/printf_info', array($result));
        }else{
            $this->display();
        }

        if(IS_AJAX){
            $this->ajaxReturn($result);
        }

    }
    /**--退款查询--**/
    public function wxrefund(){
//        ini_set('date.timezone','Asia/Shanghai');
        error_reporting(E_ERROR);

        if(isset($_REQUEST["transaction_id"]) && $_REQUEST["transaction_id"] != ""){
            $transaction_id = $_REQUEST["transaction_id"];
            $input = new \WxPayRefundQuery();
            $input->SetTransaction_id($transaction_id);
            R('Common/printf_info', array(\WxPayApi::refundQuery($input)));
        }

        if(isset($_REQUEST["out_trade_no"]) && $_REQUEST["out_trade_no"] != ""){
            $out_trade_no = $_REQUEST["out_trade_no"];
            $input = new \WxPayRefundQuery();
            $input->SetOut_trade_no($out_trade_no);
            R('Common/printf_info', array(\WxPayApi::refundQuery($input)));
            exit();
        }

        if(isset($_REQUEST["out_refund_no"]) && $_REQUEST["out_refund_no"] != ""){
            $out_refund_no = $_REQUEST["out_refund_no"];
            $input = new \WxPayRefundQuery();
            $input->SetOut_refund_no($out_refund_no);
            R('Common/printf_info', array(\WxPayApi::refundQuery($input)));
            exit();
        }

        if(isset($_REQUEST["refund_id"]) && $_REQUEST["refund_id"] != ""){
            $refund_id = $_REQUEST["refund_id"];
            $input = new \WxPayRefundQuery();
            $input->SetRefund_id($refund_id);
            R('Common/printf_info', array(\WxPayApi::refundQuery($input)));
            exit();
        }
        $this->display();
    }
    /**--本地数据库查询订单--**/
    public function localorder(){
        $wxpay = M('wxpay');
        //查询条件整理完毕后，分页并赋值
//        var_dump(urldecode($_REQUEST['where']));
        if(!empty($_REQUEST['trade_state']) || isset($_REQUEST['page'])){
            if(isset($_REQUEST['page'])){
                $where = json_decode(urldecode($_REQUEST['where']), true);
                $page = $_REQUEST['page'];
                $this->assign('where', $_REQUEST['where']);
            }else{
                //商品类型
                $product_type = $_REQUEST['product_type'];
                if(!empty($product_type)){
                    $where['product_type'] = array('eq', $product_type);
                }
                //支付完成时间段： 20160219103708
                $start = $_REQUEST['start'].'000000';
                $end = $_REQUEST['end'].'000000';
                if($start != '000000' && $end != '000000'){
                    $where['time_end'] = array('in', (int)$start, (int)$end);
                }elseif($start != '000000'){
                    $where['time_end'] = array('egt', (int)$start);
                }elseif($end != '000000'){
                    $where['time_end'] = array('elt', (int)$end);
                }
                //用户在商户appid的唯一标识
                $openid = $_REQUEST['openid'];
                if(!empty($openid)){
                    $where['openid'] = array('eq', $openid);
                }
                //交易状态：SUCCESS / ERROR
                $trade_state = $_REQUEST['trade_state'];
                $where['trade_state'] = array('eq', $trade_state);
                //页码
                $page = 1;
                $this->assign('where', json_encode($where));
            }
            $where['page'] = $page;
            $count = $wxpay->where($where)->count();
            $data = $wxpay->where($where)->page($page, 1)->select();
            $this->assign('page', $page);
            $this->assign('count', $count);
            $this->assign('data', $data);
            $return_param = C('RETURN_PARAM');
            $this->assign('return_param', $return_param);
        }
        $this->display();
    }
    /**--下载订单--**/
    public function download(){
        //require_once "../lib/WxPay.Api.php";
        //require_once "../lib/MicroPay.php";

        if(isset($_REQUEST["bill_date"]) && $_REQUEST["bill_date"] != ""){
            $bill_date = $_REQUEST["bill_date"];
            $bill_type = $_REQUEST["bill_type"];
            $input = new \WxPayDownloadBill();
            $input->SetBill_date($bill_date);
            $input->SetBill_type($bill_type);
            $file = \WxPayApi::downloadBill($input);
            echo $file;
            //TODO 对账单文件处理
            exit(0);
        }
        $this->display();
    }
}